package LoginUtil;

import java.text.*;
import java.util.*;
import java.sql.*;

import ApptUtil.Appointment;
import CreatorUtil.Patient;

public class UserDAO 	
{
	
	// code beyond this point is only relevant if using a RDBMS
   static Connection currentCon = null;
   static ResultSet rs = null;  
	
   public static UserBean login(String username, String password) {
	
      //preparing some objects for connection 
      Statement stmt = null; 
      Statement lockout = null;
      UserBean bean = null;
      
      String searchQuery =
            "select * from login where username='"
                     + username +"'";
			    
	   try 
	   {
	      //connect to DB 
	      currentCon = ConnectionManager.getConnection();
	      stmt=currentCon.createStatement();
	      rs = stmt.executeQuery(searchQuery);	        
	      boolean more = rs.next();
		       
	      // if user does not exist set the isValid variable to false
	      if (!more) 
	      {
	         System.out.println("Sorry, you are not a registered user! Please sign up first");
	         return null;
	      } 
		        
	      //if user exists set the isValid variable to true
	      else if (more) 
	      {
	    	  if(rs.getString("password").compareTo(password) == 0)
	    	  {
	    		  if(rs.getInt("lockout")<3)
	    		  {
		    		  bean = new UserBean();
		    		  bean.setType(rs.getInt("type"));
			    	  bean.setFirstName(rs.getString("firstname"));
			    	  bean.setLastName(rs.getString("lastname"));
			    	  bean.setValid(true);
			    	  
			    	  //update lockout
			    	  lockout = currentCon.createStatement();
			    	  int holder = lockout.executeUpdate("update login set lockout = 0 where username = '" + username + "'");
	    		  }
	    	  }
	    	  else
	    	  {
	    		  if(username.compareTo("admin") != 0)
	    		  {
		    		  //correct username, wrong password
			    	  lockout = currentCon.createStatement();
			    	  int holder = lockout.executeUpdate("update login set lockout = lockout+1 where username = '" + username + "'");
	    		  }
	    	  }
	    	  
	      }
	   }
	
	   catch (Exception ex) 
	   {
	      System.out.println("Log In failed: An Exception has occurred! " + ex);
	   } 
		    
	   //some exception handling
	   finally 
	   {
	      if (rs != null)	{
	         try {
	            rs.close();
	         } catch (Exception e) {}
	            rs = null;
	         }
		
	      if (stmt != null) {
	         try {
	            stmt.close();
	         } catch (Exception e) {}
	            stmt = null;
	         }
		
	      if (currentCon != null) {
	         try {
	            currentCon.close();
	         } catch (Exception e) {
	         }
	
	         currentCon = null;
	      }
	   }
	   
	
	return bean;
		
	   }
   
   public static boolean insertNewPatient(Patient user)
   {
	   boolean bool = false;
	   Statement stmt = null; 
	   int newID;
	   String loginInsert = "insert into login (username,password,firstname,lastname,type,lockout) values('" + user.getUsername() + "','"
	   			+ user.getPassword() +"','" + user.getFirstName() +"','" + user.getLastName() +"'," + Integer.toString(user.getTypeForDB()) 
	   			+ ",0)";
	   
	   currentCon = ConnectionManager.getConnection();
	      try 
	      {
	    	  stmt=currentCon.createStatement();
		      int rows = stmt.executeUpdate(loginInsert);
		      ResultSet rs = stmt.executeQuery("select LAST_INSERT_ID()");
		      if(rs.next())
				{
					newID = rs.getInt(1);
					String patInsert = "insert into patient (user_id, address, phone, email, gender, pharmacy, insurance, allergies, age) values(" 
						+ Integer.toString(newID) + ",'" + user.getAddress() +"','" + user.getPhone() +"','" + user.getEmail() +"','" 
						+ user.getGender() +"','" + user.getPharmacy() +"','" + user.getInsurance()+ "','" + user.getAllergies() + "'," +
						Integer.toString(user.getAge()) + ")";
					rows = stmt.executeUpdate(patInsert);
					bool = true;
				}
	      }
	      catch (SQLException e) {
			e.printStackTrace();
		}
	      
	      finally 
		   {
		      if (rs != null)	{
		         try {
		            rs.close();
		         } catch (Exception e) {}
		            rs = null;
		         }
			
		      if (stmt != null) {
		         try {
		            stmt.close();
		         } catch (Exception e) {}
		            stmt = null;
		         }
			
		      if (currentCon != null) {
		         try {
		            currentCon.close();
		         } catch (Exception e) {
		         }
		
		         currentCon = null;
		      }
		   }
	   
	   
	   return bool;
   }
   
   public static boolean insertNewDocNurse(UserBean user)
   {
	   boolean bool = false;
	   Statement stmt = null; 
	   String loginInsert = "insert into login (username,password,firstname,lastname,type,lockout) values('" + user.getUsername() + "','"
	   			+ user.getPassword() +"','" + user.getFirstName() +"','" + user.getLastName() +"'," + Integer.toString(user.getTypeForDB()) 
	   			+ ",0)";
	   
	   currentCon = ConnectionManager.getConnection();
	   try 
	      {
	    	  stmt=currentCon.createStatement();
		      int rows = stmt.executeUpdate(loginInsert);
		      bool = true;
	      }
	      catch (SQLException e) {
			e.printStackTrace();
		}
	      
	      finally 
		   {
		      if (rs != null)	{
		         try {
		            rs.close();
		         } catch (Exception e) {}
		            rs = null;
		         }
			
		      if (stmt != null) {
		         try {
		            stmt.close();
		         } catch (Exception e) {}
		            stmt = null;
		         }
			
		      if (currentCon != null) {
		         try {
		            currentCon.close();
		         } catch (Exception e) {
		         }
		
		         currentCon = null;
		      }
		   }
	   
	   
	   return bool;
   }
   
   public static boolean insertAppt(Appointment app, Patient user)
   {
	   boolean bool = false;
	   
	   Statement stmt = null; 
	   String loginInsert = "insert into appt (username,password,firstname,lastname,type,lockout) values('" + user.getUsername() + "','"
	   			+ user.getPassword() +"','" + user.getFirstName() +"','" + user.getLastName() +"'," + Integer.toString(user.getTypeForDB()) 
	   			+ ",0)";
	   
	   currentCon = ConnectionManager.getConnection();
	   try 
	      {
	    	  stmt=currentCon.createStatement();
		      int rows = stmt.executeUpdate(loginInsert);
		      bool = true;
	      }
	      catch (SQLException e) {
			e.printStackTrace();
		}
	      
	      finally 
		   {
		      if (rs != null)	{
		         try {
		            rs.close();
		         } catch (Exception e) {}
		            rs = null;
		         }
			
		      if (stmt != null) {
		         try {
		            stmt.close();
		         } catch (Exception e) {}
		            stmt = null;
		         }
			
		      if (currentCon != null) {
		         try {
		            currentCon.close();
		         } catch (Exception e) {
		         }
		
		         currentCon = null;
		      }
		   }
	   
	   
	   return bool;
   }
}